<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Teams extends CI_Controller
{
	function __construct()
	{
		parent::__construct();		
		if (!$this->tank_auth->is_logged_in()) redirect('auth/login');				
		
		//load models
		$this->load->model('Teams_db');
	}
	
	/*VIEW FUNCTIONS*/
	function index()
	{	
		redirect('teams/manage');
	}
	function add()
	{
		//default
		$data['ptitle'] = "Add Team";				
		$data['username'] = $this->tank_auth->get_username();
		$data['module'] = 'teams';
		$data['active_header'] = 'admin';
		//end default
		
		//dropdowns
		$data['groups'] = $this->Teams_db->getTeams();
		
		$data['content'] = $this->load->view('add',$data,true);		
		$this->load->view('default_view',$data);
	}
	
	function manage()
	{
		//default
		$data['ptitle'] = "Manage Teams";				
		$data['username'] = $this->tank_auth->get_username();
		$data['module'] = 'teams';
		$data['active_header'] = 'admin';
		//end default		
		
		$data['content'] = $this->load->view('manage',$data,true);		
		$this->load->view('default_view',$data);
	}
	
	function edit($id)
	{		
		//default
		$data['ptitle'] = "Edit Team";				
		$data['username'] = $this->tank_auth->get_username();
		$data['module'] = 'teams';
		$data['active_header'] = 'admin';
		//end default
		
		//dropdowns
		$data['groups'] = $this->Teams_db->getTeams(array("group_id != "=>$id));
		
		//check team if existing
		$emp = $this->Teams_db->getTeams(array('group_id'=>$id));
		if(!count($emp)) show_404('page');
		
		$data['item'] = $id;		
		$data['content'] = $this->load->view('edit',$data,true);		
		$this->load->view('default_view',$data);
		$this->load->view('template/dialog');
	}
	/*END VIEW FUNCTIONS*/
	
	
	/* AJAX FUNCTIONS */
	function addItem()
	{
		$dt = null;
		$error = false;
		foreach($_POST as $name => $value) {
			if (isset($_POST[$name])) $dt[$name] = $this->input->post($name);
		}
		
		//set additional data
		$now = date('Y-m-d H:i:s');
		$dt['created'] = $now;
		$dt['created_by'] = $dt['modified_by'] = $this->tank_auth->get_user_id();		
		
		$item = $this->Teams_db->save($dt);
		
		//audit trail
		$this->Audittrail_db->save(array('module'=>'teams', 'item_id'=>$item, 'action'=>'add', 'description'=>serialize($dt), 'created'=>$now, 'created_by'=>$this->tank_auth->get_user_id()));
		
		echo json_encode(array('return'=>'success'));
		return;
	}
	
	function viewItem()
	{
		$dt['group_id'] = $this->input->post('item');
		$details = $this->Teams_db->getTeams($dt);
		if ($details)
		{
			//$details[0]['date_hired'] = ($details[0]['date_hired']) ? $this->members_pi->convertDateDisplay($details[0]['date_hired'],false,'') : '';					
			echo json_encode($details);
		}
	}
	
	function updateItem()
	{	
		$id = $this->input->post('item');		
		$dt = null;
		$error = false;
		foreach($_POST as $name => $value) {
			if($name!='item')
				if (isset($_POST[$name])) $dt[$name] = $this->input->post($name);
		}		
					
		//set additional data
		$dt['modified'] = date('Y-m-d H:i:s');
		$dt['modified_by'] = $this->tank_auth->get_user_id();		
		$item = $this->Teams_db->save($dt, $id);
		
		
		//audit trail
		$this->Audittrail_db->save(array('module'=>'teams', 'item_id'=>$item, 'action'=>'update', 'description'=>serialize($dt), 'created'=>$dt['modified'], 'created_by'=>$this->tank_auth->get_user_id()));
		
		echo json_encode(array('return'=>'success'));
		return;
	}
	
	function deleteItem()
	{
		$dt = $this->input->post('items');
		
		$item = $this->Teams_db->delete($dt);
		
		//audit trail
		foreach($dt as $d)
			$this->Audittrail_db->save(array('module'=>'teams', 'item_id'=>$d, 'action'=>'delete', 'description'=>serialize($dt), 'created'=>date('Y-m-d H:i:s'), 'created_by'=>$this->tank_auth->get_user_id()));
		
		echo json_encode(array('return'=>'success'));
		return;
	}
	
	function listview()
	{
		$data = $this->Teams_db->getList();
		echo json_encode($data);
	}
	/* END AJAX FUNCTIONS */
	
	
	
	
	/* EMPLOYEE TEAM */
	function listViewTeamEmployee($groupID)
	{
		$data = $this->Teams_db->getListTeamEmployee(array('group_id'=>$groupID));
		echo json_encode($data);
	}
	
	function addTeamEmployee()
	{
		$dt = null;
		$error = false;
		
		//make sure no employee is duplicated
		//compare existing members to to-be-added members
		$tobeadded = $this->input->post('items');
		$groupID = $this->input->post('group_id');
		$existing = $this->Teams_db->getTeamEmployeesID(array('group_id'=>$groupID), $tobeadded);
		$count=0;
		$exist = array();
		foreach($existing as $e)
		{
			$exist[$count] = $e['user_id'];
			$count++;
		}
		
		$notYetMembers = array_diff($tobeadded, $exist);

		$members = array();
		$now = date('Y-m-d H:i:s');
		foreach($notYetMembers as $s)
		{
			$member = array('user_id'=>$s, 
				'group_id'=>$groupID,
				'created'=>$now, 
				'created_by'=>$this->tank_auth->get_user_id(), 
				'modified_by'=>$this->tank_auth->get_user_id());
			array_push($members, $member);
		}
		if(count($members))
		{
			$this->Teams_db->saveTeamEmployee($members);
			
			//audit trail
			$this->Audittrail_db->save(array('module'=>'teamemployees', 'item_id'=>$groupID, 'action'=>'add', 'description'=>serialize($dt), 'created'=>$now, 'created_by'=>$this->tank_auth->get_user_id()));
		}
		echo json_encode(array('return'=>'success'));
		return;
	}
	
	function deleteTeamEmployee()
	{
		$dt = $this->input->post('items');
		
		$item = $this->Teams_db->deleteTeamEmployee($dt);
		
		//audit trail
		foreach($dt as $d)
			$this->Audittrail_db->save(array('module'=>'teamemployees', 'item_id'=>$d, 'action'=>'delete', 'description'=>serialize($dt), 'created'=>date('Y-m-d H:i:s'), 'created_by'=>$this->tank_auth->get_user_id()));
		
		echo json_encode(array('return'=>'success'));
		return;
	}
	
	function updateTeamEmployee()
	{	
		$id = $this->input->post('employee_group_id');
		$is_manager = $this->input->post('is_manager');
		$error = false;
					
		//set additional data
		$dt['modified'] = date('Y-m-d H:i:s');
		$dt['modified_by'] = $this->tank_auth->get_user_id();		
		$item = $this->Teams_db->saveTeamEmployee(array('is_manager'=>$is_manager), $id);
		
		
		//audit trail
		$this->Audittrail_db->save(array('module'=>'teamemployees', 'item_id'=>$item, 'action'=>'update', 'description'=>serialize($dt), 'created'=>$dt['modified'], 'created_by'=>$this->tank_auth->get_user_id()));
		
		echo json_encode(array('return'=>'success'));
		return;
	}
	
	
	/* EMPLOYEE TEAM */
	function listViewTeamApprover($groupID)
	{
		$data = $this->Teams_db->getListTeamApprover(array('group_id'=>$groupID));
		echo json_encode($data);
	}
	
	function addTeamApprover()
	{
		$dt = null;
		$error = false;
		
		//make sure no employee is duplicated
		//compare existing members to to-be-added members
		$tobeadded = $this->input->post('items');
		$groupID = $this->input->post('group_id');
		$existing = $this->Teams_db->getTeamApproversID(array('group_id'=>$groupID), $tobeadded);
		$count=0;
		$exist = array();
		foreach($existing as $e)
		{
			$exist[$count] = $e['user_id'];
			$count++;
		}
		
		$notYetMembers = array_diff($tobeadded, $exist);

		$members = array();
		$now = date('Y-m-d H:i:s');
		foreach($notYetMembers as $s)
		{
			$member = array('user_id'=>$s, 
				'group_id'=>$groupID,
				'created'=>$now, 
				'created_by'=>$this->tank_auth->get_user_id(), 
				'modified_by'=>$this->tank_auth->get_user_id());
			array_push($members, $member);
		}
		if(count($members))
		{
			$this->Teams_db->saveTeamApprover($members);
			
			//audit trail
			$this->Audittrail_db->save(array('module'=>'teamapprover', 'item_id'=>$groupID, 'action'=>'add', 'description'=>serialize($dt), 'created'=>$now, 'created_by'=>$this->tank_auth->get_user_id()));
		}
		echo json_encode(array('return'=>'success'));
		return;
	}
	
	function deleteTeamApprover()
	{
		$dt = $this->input->post('items');
		
		$item = $this->Teams_db->deleteTeamApprover($dt);
		
		//audit trail
		foreach($dt as $d)
			$this->Audittrail_db->save(array('module'=>'teamemployees', 'item_id'=>$d, 'action'=>'delete', 'description'=>serialize($dt), 'created'=>date('Y-m-d H:i:s'), 'created_by'=>$this->tank_auth->get_user_id()));
		
		echo json_encode(array('return'=>'success'));
		return;
	}
	
	function updateTeamApprover()
	{	
		$id = $this->input->post('employee_group_id');
		$is_manager = $this->input->post('is_manager');
		$error = false;
					
		//set additional data
		$dt['modified'] = date('Y-m-d H:i:s');
		$dt['modified_by'] = $this->tank_auth->get_user_id();		
		$item = $this->Teams_db->saveTeamApprover(array('is_manager'=>$is_manager), $id);
		
		
		//audit trail
		$this->Audittrail_db->save(array('module'=>'teamemployees', 'item_id'=>$item, 'action'=>'update', 'description'=>serialize($dt), 'created'=>$dt['modified'], 'created_by'=>$this->tank_auth->get_user_id()));
		
		echo json_encode(array('return'=>'success'));
		return;
	}
	
	
	/* LOAD MODULE JS */
	function js() {
        $this->load->view('js/script.js');
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */